/**
 * Created by lumix on 2015/4/22.
 */
var express = require('express');
var router = express.Router();
var Promise = require('promise');
var logger = require('log4js').getLogger('comment');
var commonUtils = require('./../../utils/commonUtils');
var logFilter = require('./../../utils/logFilter');
var commentService = require('../service/CommentService')
var memberService = require('../service/MemberService')
var articleService = require('../service/ArticleService')
router.post('/query',logFilter.saveCommentLog ,function (req, res, next) {
    var pageNo = req.body.pageNo;
    var type = req.body.type;
    var articleId = req.body.articleId;
    var memberId = req.body.memberId;
    if(!pageNo){pageNo = 1;}
    if(!articleId){
        res.json({'success':false,'msg':'参数缺失'});
        return
    }
    var pageSize = 16;
    commentService.queryForPage(articleId,type,pageNo,pageSize).then(function(coms){
            var queue = [];
            coms.forEach(function (item) {
                var memberId = item.memberId;

                queue.push(memberService.getById(memberId).then(function (m) {
                    item.username = m.username;
                    item.headImgUrl = m.headImgUrl;
                    return Promise.resolve(item);
                }))

            })

        return Promise.all(queue);
    }).then(function (coms) {
        var data = {};
        data.comments = coms;
        res.json({'success':true,'data':data});

    },function(err){
        console.log(err);
        logger.error(err);
        res.json({'success':false,'msg':'网络异常'});
    })

})

router.post('/delete', function (req, res, next) {

    var id = req.body.id;
    if(!id){
        res.json({'success':false,'msg':'参数缺失'});
        return;
    }
    commentService.getById(id).then(function (c) {
        if(c){
            if(c.bak1==0){
                return articleService.getById(c.articleId).then(function (ar) {
                    if(ar){
                        ar.commentNum--;
                        ar.updateTime = new Date();
                        return articleService.update(ar);
                    }else{
                        return Promise.resolve(id);
                    }
                })
            }else{
                return Promise.resolve(id);
            }
        }else{
            return Promise.resolve(null);
        }
    }).then(function () {
        commentService.delete(id).then(function (r) {
            if(r){
                res.json({'success':true});
            }else{
                res.json({'success':false,'msg':"操作失败"});
            }
        })
    }, function (err) {
        console.log(err);
        logger.error(err);
        res.json({'success':false,'msg':'网络异常'});
    })
})


router.post('/tipoffRecord', function (req,res,next) {
    var id = req.body.id;
    var content = req.body.content;
    if(!id||!content){
        res.json({'success':false,'msg':'参数缺失'});
        return;
    }
    articleService.tipoffRecord(id,content).then(function (result) {
        res.json({'success':true});
    }, function (err) {
        res.json({'success':false,'msg':"操作失败"});
    })
})
module.exports = router;
